﻿using Avalonia.Platform.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AvaloniaMVVM.Custom.Dialog
{
    public static class DialogHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="title"></param>
        /// <param name="selectMany"></param>
        /// <returns></returns>
        public static async Task<IEnumerable<string>?> OpenFileDialogAsync(this object? context, string? title = null, bool selectMany = true)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }
            var topLevel = DialogManager.GetTopLevelForContext(context);
            if (topLevel != null)
            {
                var options = new FilePickerOpenOptions()
                {
                    AllowMultiple = selectMany,
                    Title = title ?? "Select any file(s)"
                };
                var storageFiles = await topLevel.StorageProvider.OpenFilePickerAsync(options);
                return storageFiles.Select(s => s.Name);
            }
            return null;
        }
    }
}
